home *** CD-ROM | disk | FTP | other *** search
- /*
- * deltree.cmm
- *
- * CEnvi shell command 'deltree'. Kills an entire directory.
- */
-
- #include "Netware.lib"
-
- usage()
- {
- printf("Use the DELTREE command to delete a directory and all of its contents.\n");
- printf("Syntax:\n");
- printf(" DELTREE [drive:][path]filename [/Y]\n");
- printf("where:\n");
- printf(" drive:/path/filename Specifies the directory you want to delete.\n");
- printf(" /Y Suppresses prompting to confirm you want to\n");
- printf(" delete the subdirectory.\n");
- printf("\nNote: Use DELTREE cautiously. Every file and subdirectory within the\n");
- printf("specified directory will be deleted.\n");
- exit(EXIT_FAILURE);
- }
-
- DeleteDirectory(pDirSpec)
- {
- lReg.ah = 0x3A;
- if !defined(_DOS32_)
- lReg.ds = segment(pDirSpec), lReg.dx = offset(pDirSpec);
- else
- lReg.dx = pointer(pDirSpec);
- return interrupt(0x21,lReg);
- }
-
- myrmdir(dir)
- {
- if( defined(_NWNLM_) ) return rmdir(dir);
- if( defined(_OS2_) )
- {
- #define ORD_DOS32DELETEDIR 226
- return DynamicLink("DOSCALLS",ORD_DOS32DELETEDIR,BIT32,CDECL,dir);
- }
- if( defined(_NTCON_) || defined(_NTWIN_) )
- {
- return !DynamicLink("KERNEL32","RemoveDirectoryA",STDCALL,dir);
- }
- if( defined(_DOS_) || defined(_WINDOWS_) || defined(_DOS32_) )
- {
- return !DeleteDirectory(dir);
- }
-
- printf("Don't know how to remove a dir in this version of CEnvi.\n");
- return 1;
- }
-
- unprotect(file)
- {
- if( defined(_NWNLM_) )
- {
- temp[0] = DOSTimeFromCalendar(time());
-
- code = NLMLink("SetFileInfo",
- file.name,
- 0x06,
- file.attrib & ~_A_RDONLY,
- tempcreate,
- tempaccess,
- tempdate,
- tempbackup,
- file.uid);
- if( code )
- printf("\nError setting attributes for file %s\n",file.name);
- return;
- }
- if( defined(_DOS_) || defined(_DOS32_) || defined(_WINDOWS_) )
- {
- SetFileAttributes(file.name,file.attrib & ~_A_RDONLY);
- return;
- }
- if( defined(_NTCON_) || defined(_NTWIN_) )
- {
- DynamicLink("KERNEL32","SetFileAttributesA",STDCALL,file.name,
- file.attrib & ~_A_RDONLY);
- return;
- }
- if( defined(_OS2_) )
- {
- }
- }
-
- /* ---------------------------------------------------------------------- */
-
- dir = "";
- are_you_sure = TRUE;
-
- parse_command_line(argc,argv)
- {
- for( i=1;i<argc;i++ )
- {
- if( argv[i][0]=='-' || argv[i][0]=='/' )
- {
- switch( toupper(argv[i][1]) )
- {
- default: usage();
- case 'Y': are_you_sure = FALSE; break;
- }
- } else {
- if( dir[0]=='\0' )
- dir = argv[i];
- else
- {
- printf("You may not specify multiple directories.\n");
- exit(EXIT_FAILURE);
- }
- }
- }
- }
-
- /* ---------------------------------------------------------------------- */
-
- /*
- * Remove the contents of a directory, then remove it
- */
- kill_directory(dirname)
- {
- strcpy(killdir,dirname);
-
- if( defined(_NWNLM_) )
- strcat(killdir,"/*.*");
- else
- strcat(killdir,"\\*.*");
-
- to_kill = Directory(killdir);
- for( i=0;to_kill && i<=GetArraySpan(to_kill);i++ )
- {
- if( to_kill[i].attrib & _A_SUBDIR )
- {
- kill_directory(to_kill[i].name);
- } else {
- if( remove(to_kill[i].name) )
- {
- unprotect(to_kill[i]);
- if( remove(FullPath(to_kill[i].name)) )
- printf("Unable to delete file \"%s\".\n",FullPath(to_kill[i].name));
- }
- }
- }
- if( myrmdir(dirname) )
- {
- printf("Unable to remove directory \"%s\".\n",dirname);
- }
- }
-
- /* --------------------------------------------------------------------------- */
-
- main(argc,argv)
- {
- // First thing to do is parse the command line.
-
- parse_command_line(argc,argv);
-
- to_kill = Directory(dir);
- if( to_kill==NULL )
- {
- printf("No such file or directory \"%s\".\n",dir);
- exit(EXIT_FAILURE);
- }
- if( GetArraySpan(to_kill)>0 )
- {
- printf("You must specify a single directory.\n");
- exit(EXIT_FAILURE);
- }
- if( (to_kill[0].attrib & _A_SUBDIR)==0 )
- {
- printf("You may only DELTREE a directory.\n");
- exit(EXIT_FAILURE);
- }
- if( are_you_sure )
- {
- printf("Delete \"%s\" and all its contents; Are you sure? (Y/N) ",dir);
- ch = getch();
- printf("%c\n",ch);
- if( toupper(ch)!='Y' ) exit(EXIT_FAILURE);
- }
-
- kill_directory(dir);
- }
-